﻿using Cavy.UdpClient.Serializable;

namespace Cavy.UdpClient
{
    public struct Message_JoinRoomRequest : IMessage
    {
        public int MessageType => (int) MessageTypeEnum.JoinRoomRequest;
        
        public void Serialize(NetDataWriter writer)
        {
            
        }

        public void Deserialize(NetDataReader reader)
        {
            
        }

        public void AnalysisData(MessageFactory factory, IpEndPoint endPoint)
        {
            var room = factory.As<UdpRoom>();
            bool hasSet = false;
            for (int i = 0; i < room._connections.Count; i++)
            {
                if (room._connections[i].Address == endPoint.Address &&
                    room._connections[i].Port == endPoint.Port)
                {
                    hasSet = true;
                }
            }

            if (!hasSet) room._connections.Add(endPoint);
            room.Transport.Send(new Message_JoinRoomResponse() { Success = true }, endPoint);
        }
    }
}